package com.wtwd.wtwddevice.listener.deviceServerMsg.handler.impl;

import com.wtwd.device.annotation.MsgType;
import com.wtwd.device.constant.AnswerStateEnum;
import com.wtwd.device.constant.MsgTypeEnum;
import com.wtwd.device.mqtentity.MessageBase;
import com.wtwd.device.protocol.AppProtocol;
import com.wtwd.device.protocol.WTProtocol;
import com.wtwd.wtwddevice.listener.deviceServerMsg.handler.IMsgHandler;
import com.wtwd.wtwddevice.service.IAppCommService;
import com.wtwd.wtwddevice.service.IDeviceService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@MsgType(MsgTypeEnum.REMOTE_OPERATE_TERMINAL)
@Component
public class SetRemoteOperateTerminalHandlerImpl implements IMsgHandler {

    @Resource
    private IDeviceService deviceService;

    @Resource
    private IAppCommService appCommService;


    @Override
    public void handleMsg(MessageBase messageBase) {
        WTProtocol wtProtocol = messageBase.getWtProtocol();
        Integer state = Integer.valueOf(wtProtocol.getBody());
        Long deviceId = deviceService.getDeviceIdByImei(wtProtocol.getImei());
        if (state.equals(AnswerStateEnum.NORMAL.getState())){
            AppProtocol protocol = AppProtocol.builder()
                    .imei(wtProtocol.getImei())
                    .cmdName(MsgTypeEnum.APP_SET_REMOTE_OPERATE_TERMINAL.getCmdName())
                    .state(AnswerStateEnum.NORMAL.getState())
                    .data(null)
                    .build();
            appCommService.pushDeviceState(deviceId, protocol);
        }
    }

}
